//
//  BNDOfficeDocSearchView.m
//  OfficeDoc
//
//  Created by pie on 2022/4/1.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDOfficeDocSearchView.h"


@interface BNDOfficeDocSearchView ()<UITextFieldDelegate>
@property (nonatomic, strong) UIView * showView;
@property (nonatomic, strong) UITextField * searchTextField;
@end

@implementation BNDOfficeDocSearchView

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColor.whiteColor;
        _showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 34)];
        _showView.backgroundColor = UIColorWithHex(0xF6F6F8);
        _showView.layer.cornerRadius = 8;
        [self addSubview:_showView];
        [_showView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.height.mas_equalTo(38);
            make.centerY.mas_equalTo(self);
        }];
        
        UIImageView *tipImageView = [[UIImageView alloc] initWithImage:BNDImageNamed(@"bnd_company_tip")];
        [self.showView addSubview:tipImageView];
        [tipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.showView).offset(8);
            make.centerY.mas_equalTo(self.showView);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        
        [_showView addSubview:self.searchTextField];
        [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(tipImageView.mas_right).offset(8);
            make.right.mas_equalTo(self.showView.mas_right).offset(-8);
            make.centerY.mas_equalTo(self.showView);
            make.height.mas_equalTo(34);
        }];
        
      
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:_searchTextField];
    
    }
    
    return self;
}

- (UITextField*)searchTextField
{
    if(!_searchTextField){
        _searchTextField = [[UITextField alloc] init];
        _searchTextField.textColor = UIColorWithHex(0x34363B);
        [_searchTextField setReturnKeyType:UIReturnKeySearch];
        [_searchTextField setFont:BNDPFRegularFont(14)];
        [_searchTextField setDelegate:self];
        UIColor *color =  UIColorWithHex(0xB4B8BE) ;
        _searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索公告标题、内容" attributes:@{NSForegroundColorAttributeName: color}];
        [_searchTextField setClearButtonMode:UITextFieldViewModeAlways];
    }
    return _searchTextField;
}



#pragma mark -- UITextFieldDelegate -- 限制字数

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL basicTest = [string isEqualToString:@" "];
    if(basicTest){
        return NO;
    }
    
    if ([string isEqualToString:@"\n"]){
        return NO;
    }
    
    NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([toBeString length] > 30)
    {
        return NO;
    }
    
    return YES;
}

- (void)textFieldDidChange:(NSNotification *)note
{
    UITextField *textField = note.object;
    BND_BLOCK_EXEC(self.searchBlock,textField.text);
   
}
@end
